<?php
/**
 * Created by PhpStorm.
 * User: caoguangyang
 * Date: 2023-06-17
 * Time: 16:01
 */

namespace App\Http\Controllers\Wx;

use App\CodeResponse;
use App\Services\Goods\CategoryServices;
use Illuminate\Http\Request;

class CategoryController extends WxController {
    protected $only = [];

    /**
     * 获取类目列表
     * @param Request $request
     * @return array
     */
    public function index(Request $request) {
        // 获取id
        $id = $request->input("id", 0);
        // 获取所有一级类目
        $l1List = CategoryServices::getInstance()->getL1List();
        // 获取当前类目
        if (empty($id)) {
            $current = $l1List->first();
        } else {
            $current = $l1List->where("id", $id)->first();
        }
        // 获取二级类目
        $l2List = null;
        if (!is_null($current)) {
            $l2List = CategoryServices::getInstance()->getL2ListByPid($current->id);
        }
        return [
            "categoryList" => $l1List->toArray(),
            "currentCategory" => $current,
            "currentSubCategory" => $l2List->toArray()
        ];
    }

    /**
     * 获取当前类目及子类目
     * @param Request $request
     * @return array
     */
    public function current(Request $request) {
        $id = $request->input("id", 0);
        if (empty($id)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        // 获取当前一级类目
        $category = CategoryServices::getInstance()->getL1ById($id);
        if (is_null($category)) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $l2List = CategoryServices::getInstance()->getL2ListByPid($category->id);

        return [
          "currentCategory" => $category,
          "currentSubCategory" => $l2List->toArray()
        ];
    }
}